import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../../domain/providers/album_provider.dart';
import '../../../domain/providers/backup_provider.dart';
import 'album_item.dart';

/// 相册列表组件
class AlbumList extends StatelessWidget {
  const AlbumList({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer2<AlbumProvider, BackupProvider>(
      builder: (context, albumProvider, backupProvider, _) {
        final loadingStatus = albumProvider.status;

        // 显示加载状态
        if (loadingStatus == AlbumLoadingStatus.loading) {
          return _buildLoadingView(albumProvider);
        }

        // 显示错误状态
        if (loadingStatus == AlbumLoadingStatus.error) {
          return _buildErrorView(context, albumProvider);
        }

        // 无数据状态
        if (albumProvider.albums.isEmpty) {
          return _buildEmptyView(context, albumProvider);
        }

        // 显示相册列表
        return _buildAlbumListView(context, albumProvider, backupProvider);
      },
    );
  }

  /// 构建加载中视图
  Widget _buildLoadingView(AlbumProvider albumProvider) {
    // 如果有部分相册已加载，显示加载进度和已加载相册
    if (albumProvider.albums.isNotEmpty) {
      return Column(
        children: [
          _buildLoadingProgressBar(albumProvider),
          Expanded(
            child: ListView.builder(
              itemCount: albumProvider.albums.length,
              itemBuilder: (context, index) {
                final album = albumProvider.albums[index];
                return AlbumItem(
                  album: album,
                  onSelectionChanged: (selected) {
                    albumProvider.setAlbumSelected(album.id, selected);
                  },
                );
              },
            ),
          ),
        ],
      );
    }

    // 否则显示加载指示器
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const CircularProgressIndicator(),
          const SizedBox(height: 16),
          Text(
            '正在加载设备相册 (${albumProvider.loadedCount}/${albumProvider.totalCount})',
          ),
        ],
      ),
    );
  }

  /// 构建加载进度条
  Widget _buildLoadingProgressBar(AlbumProvider albumProvider) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0),
      child: Column(
        children: [
          LinearProgressIndicator(value: albumProvider.loadingProgress / 100),
          Padding(
            padding: const EdgeInsets.symmetric(
              vertical: 4.0,
              horizontal: 16.0,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  '加载相册中: ${albumProvider.loadedCount}/${albumProvider.totalCount}',
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 构建错误视图
  Widget _buildErrorView(BuildContext context, AlbumProvider albumProvider) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Icon(Icons.error_outline, size: 48, color: Colors.red),
          const SizedBox(height: 16),
          Text('加载相册失败: ${albumProvider.errorMessage}'),
          const SizedBox(height: 16),
          ElevatedButton(
            onPressed: () {
              albumProvider.loadAlbums();
            },
            child: const Text('重试'),
          ),
        ],
      ),
    );
  }

  /// 构建空视图
  Widget _buildEmptyView(BuildContext context, AlbumProvider albumProvider) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Icon(Icons.photo_album_outlined, size: 48, color: Colors.grey),
          const SizedBox(height: 16),
          const Text('没有找到相册'),
          const SizedBox(height: 16),
          ElevatedButton(
            onPressed: () {
              albumProvider.forceRefreshAlbums();
            },
            child: const Text('刷新'),
          ),
        ],
      ),
    );
  }

  /// 构建相册列表视图
  Widget _buildAlbumListView(
    BuildContext context,
    AlbumProvider albumProvider,
    BackupProvider backupProvider,
  ) {
    return Column(
      children: [
        // 全选按钮
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 16.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              TextButton(
                onPressed: () {
                  // 检查是否所有相册都已选择
                  final allSelected = albumProvider.albums.every(
                    (album) => album.isSelected,
                  );
                  // 切换全选/取消全选
                  albumProvider.selectAllAlbums(!allSelected);
                },
                child: Text(
                  albumProvider.albums.every((album) => album.isSelected)
                      ? '取消全选'
                      : '全选',
                ),
              ),
            ],
          ),
        ),

        // 相册列表
        Expanded(
          child: ListView.builder(
            itemCount: albumProvider.albums.length,
            itemBuilder: (context, index) {
              final album = albumProvider.albums[index];
              return AlbumItem(
                album: album,
                onSelectionChanged: (selected) {
                  albumProvider.setAlbumSelected(album.id, selected);
                },
              );
            },
          ),
        ),
      ],
    );
  }
}
